package com.lilicould.controller;

import com.lilicould.entity.ContractDetails;
import com.lilicould.entity.Employee;
import com.lilicould.entity.EmployeeDetails;
import com.lilicould.service.impl.ContractServiceImpl;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.util.ArrayList;

@WebServlet("/contract/list")
public class ContractListController extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        EmployeeDetails employeeDetails = (EmployeeDetails) req.getSession().getAttribute("employeeDetails");
        Employee employee = employeeDetails.getEmployee();
        ContractServiceImpl contractService = new ContractServiceImpl();
        ArrayList<ContractDetails> allContractDetails = contractService.selectAllContractDetails();
        ArrayList<ContractDetails> contractDetails = new ArrayList<>();

        // 筛选出当前经理签订的合同详情
        for (ContractDetails contractDetail : allContractDetails) {
            if (contractDetail.getLeaseContract().getManagerId().equals(employee.getEmployeeId())) {
                contractDetails.add(contractDetail);
            }
        }
        req.getSession().setAttribute("contractDetails", contractDetails);
        req.getRequestDispatcher("/page/emp/contract.jsp").forward(req, resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }
}
